/**
 * @date 2020年10月14日 15点34分
 * @author hanshuailing
 * 新融云消息服务
 * 链接融云和处理消息
 */
 import { Injectable } from '@angular/core';
 import { environment } from '../../environments/environment';

 declare var RongIMLib: any;
 @Injectable({
     providedIn: 'root'
 })
 export class MessageNewService {
     public joinRoomId: string;//加入的房间号
     public userId: number;//加房的uid
     public ryToken: string;
     public ryIM: any;
     public publicMessageRoom: string = 'publicMessageRoom';
     constructor(

     ) { }
     //加入聊天室
     public joinFirstRoom(rcMessage: any) {
         var chatRoom = this.ryIM.ChatRoom.get({
             id: this.joinRoomId + ""
         });
         let that = this;
         chatRoom.join({
             count: -1 // 进入后, 自动拉取 20 条聊天室最新消息
         }).then(function () {
             console.log('加入聊天室成功');


         });
     }

     //加入公共聊天室 191、111和147
     public joinSecondRoom(rcMessage: any) {
         var chatRoom = this.ryIM.ChatRoom.get({
             id: this.publicMessageRoom
         });
         chatRoom.join({
             count: -1 // 进入后, 自动拉取 20 条聊天室最新消息
         }).then(function () {
             console.log('加入公共聊天室成功');
         });

     }
     /**
      * 初始化融云消息
      * @param params 需要的参数
      * @param rcMessage 回调消息的方法
      */
     public initMessage(params: any, rcMessage: any) {
         let config = {
             appkey: params.appKey,
             detect: {
                 url: "https://cdn.ronghub.com/im_detecting",
                 intervalTime: 1500
             }
         }
         this.joinRoomId = params.roomId;
         var im = RongIMLib.init(config);
         this.ryIM = im;
         var user = {
             token: params.token
         };
         let that = this;
         im.connect(user).then(function (user: any) {
             that.joinFirstRoom(rcMessage);
             that.joinSecondRoom(rcMessage);

         }).catch(function (error) {
             im.reconnect().then(function (user) {
                 console.log('重新链接成功, 链接用户 id 为: ', user.id);
             }).catch(function (error) {
                 console.log('链接失败: ', error.code, error.msg);
             });
             console.log('链接失败: ', error.code, error.msg);
         });


         im.watch({
             message: function (event) {
                 var message = event.message;
                 that.receiveNewMessage(message, rcMessage);
             },
             status: function (event) {
                 var status = event.status;
                 if (status == 6) {
                     im.reconnect().then(function (user) {
                         console.log('重新链接成功, 链接用户 id 为: ', user.id);
                     }).catch(function (error) {
                         console.log('链接失败: ', error.code, error.msg);
                     });
                 }
                 console.log('连接状态码:', status);
             },
         });
     }



     /**
      * 接收消息并回调
      * @param message
      * @param rc
      */
     public receiveNewMessage(message: any, rc: any) {
         if ((message.content.extra == 1056 ) &&
             (message.targetId == this.joinRoomId || message.targetId == this.publicMessageRoom)) {
             try {
                 rc({ extra: message.content.extra, content: JSON.parse(message.content.content) });

             } catch (error) {
                 console.log(error)
             }
         }
     }
 }
